package com.link.base.wechat.wxmenu.service;

import com.link.base.wechat.wxlog.model.WxLogModule;
import com.link.base.wechat.wxlog.service.WxLogService;
import com.link.base.wechat.wxmenu.model.WxMenu;
import com.link.base.wechat.wxmenu.util.WxMenuUtil;
import com.link.base.wechat.wxtoken.model.WxToken;
import com.link.base.wechat.wxtoken.service.WxTokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 微信公众号自定义菜单实现类
 *
 * @author lujiang
 * @date 2019/03/6 18:45
 */
@Service
public class WmenuServiceImpl implements WmenuService {

    @Resource
    private WxLogService wxLogService;

    @Resource
    private WxTokenService wxTokenService;

    @Override
    public int upsertWxMenu(String wxAppId, WxMenu wxMenu) throws Exception {
        WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
        int num = WxMenuUtil.upsertWxMenu(wxToken.getAccess_token(),wxMenu);
        wxLogService.log(WxLogModule.wxLogMenu,"菜单操作", "新增/删除/修改自定义菜单", wxAppId, wxToken.getCorpid());
        return num;
    }

    @Override
    public int clearWxMenu(String wxAppId) throws Exception {
        WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
        int num = WxMenuUtil.clearWxMenu(wxToken.getAccess_token());
        wxLogService.log(WxLogModule.wxLogMenu,"菜单操作","清空菜单", wxAppId, wxToken.getCorpid());
        return num;
    }
}
